package com.vm.action.system;

import java.sql.Timestamp;

import java.util.List;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Preparable;
import com.vm.action.ActionSuper;
import com.vm.dao.system.ICode;
import com.vm.entity.Code;
import com.vm.entity.User;
import com.vm.log4j.CustomizeLog4j;

@SuppressWarnings("serial")
public class CodeAction extends ActionSuper implements Preparable{
	private static Logger _log = CustomizeLog4j.getLogger(CodeAction.class);
	private Code code;
	private ICode codeDAO;
	private List<Code> listCode;
	private String classCd;

	public String getClassCd() {
		return classCd;
	}

	public void setClassCd(String classCd) {
		this.classCd = classCd;
	}

	public void prepare() throws Exception {
		super.setButtonGrpMap(codeDAO.seletButtonGrp((User)this.getSession().get("user"), "codeAction"));
	}

	public String list() throws Exception {
		listCode = codeDAO.findAllCode(classCd);
		return SUCCESS;

	}

	public String add() throws Exception {
		return SUCCESS;
	}

	public String save() throws Exception {
		String[] msgArg = new String[] { "codeAction_list.do?classCd="+code.getClassCd(),"codeAction_add.do?classCd="+code.getClassCd()};
		String msg = null;
		try {
			code.setUpdateTime(new Timestamp(System.currentTimeMillis()));
			codeDAO.save(code);
			msg = super.getResourceMessage("insert_code_success", msgArg);
		} catch (Exception e) {
			_log.error("", e);
			msg = super.getResourceMessage("insert_code_err", msgArg);
		}

		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;

	}

	public String edit() throws Exception {
		code = codeDAO.findById(code.getCodeId());
		return SUCCESS;
	}

	public String update() throws Exception {
		String[] msgArg = new String[] { "codeAction_list.do?classCd="+code.getClassCd(),"codeAction_edit.do?code.codeId="+code.getCodeId() };
		String msg = null;
		try {
			code.setUpdateTime(new Timestamp(System.currentTimeMillis()));
			codeDAO.update(code);
			msg = super.getResourceMessage("update_code_success", msgArg);
		} catch (Exception e) {
			_log.error("", e);
			msg = super.getResourceMessage("update_code_err", msgArg);
		}

		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}

	public String delete() throws Exception {
		String[] msgArg = new String[] { "codeAction_list.do?classCd="+classCd };
		String msg = null;
		try {
			codeDAO.deleteById(code.getCodeId());
			msg = super.getResourceMessage("delete_code_success", msgArg);
		} catch (Exception e) {
			_log.error("", e);
			msg = super.getResourceMessage("delete_code_err", msgArg);
		}

		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}


	public Code getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public List<Code> getListCode() {
		return listCode;
	}

	public void setListCode(List<Code> listCode) {
		this.listCode = listCode;
	}

	public ICode getCodeDAO() {
		return codeDAO;
	}

	public void setCodeDAO(ICode codeDAO) {
		this.codeDAO = codeDAO;
	}
}
